package com.wxg.gui;

import java.util.List;

import javax.swing.JTextArea;

import com.wxg.audio.MP3Player;
import com.wxg.domain.englishword.Englishword;
import com.wxg.gui.baidu.BaiduFanyiWordAudioApp;
import com.wxg.httpclient.ObtainAudioFromBaiduFanyi;
import com.wxg.httpclient.ObtainAudioFromBaiduFanyi.AudioType;
import com.wxg.httpclient.ObtainEnglishWordExplain;

public class WordSaveAndQuery {
	
	private String word;
	
	public WordSaveAndQuery(){
	}

	public void all(String text, final BaiduFanyiWordAudioApp app, AudioType audioType){
		word = text;
		
		Englishword word0 = new Englishword();
		dealwithWord(audioType, word0);
		dealwithExplains(app, word0);
	}

	/**
	 * 
	 * 这一段是查询单词释义的,从有道词典下载！
	 * 
	 * @param app
	 * @param word0
	 */
	private void dealwithExplains(final BaiduFanyiWordAudioApp app, Englishword word0) {
		ObtainEnglishWordExplain explain = new ObtainEnglishWordExplain();
		List<String> list = explain.obtainExplain(word);
		if(null != list){
			StringBuilder sbb = new StringBuilder();
			for(String str : list){
				sbb.append(str).append("\r\n");
			}
			
			JTextArea area = app.getWordExpResultPanel().getResultTextArea();
			area.setText(sbb.toString());
			
		}
	}

	/**
	 * 
	 * 这一段是查询音频的,程序会判断音频文件是否已经下载.已经下载的不会重复下载！(从百度翻译查询！)
	 * 
	 * @param audioType
	 * @param word0
	 */
	private void dealwithWord(AudioType audioType, Englishword word0) {
		String mp3FilePath = null;
		mp3FilePath = ObtainAudioFromBaiduFanyi.queryWord(word,audioType);
		MP3Player mp3 = new MP3Player(mp3FilePath);
		mp3.play();
//		{
//			word0.setWord(word);
//			if(audioType.equals(USA)){
//				word0.setUsaPath(mp3FilePath);								
//			}else if(audioType.equals(Britain)){
//				word0.setBritainPath(mp3FilePath);
//			}
//			service.addWord(word0);
//		}
	}
}
